package org.sonatype.nexus.yum.internal.capabilities;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.sonatype.nexus.yum.internal.task.CommandLineExecutor_2_14_14;

public class YumCapability_2_14_14 {

	public static void validate(final String type, final String path, final String versionConstraint) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			if (CommandLineExecutor_2_14_14.exec(path, " --version", baos, baos) == 0) {
				String versionOutput = new String(baos.toByteArray());
				
				String version = parseVersion(type, versionOutput);
				if (version == null) {
					System.out.println("WARNING: Could not determine version!");
				}
					
			} else {
				System.out.println("Execute command fail");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static String parseVersion(final String type, final String rawVersion) {
		try {
			String[] lines = rawVersion.split("\n");
			for (String line : lines) {
				line = line.trim();

				if (line.startsWith(type + " ")) {
					String[] parts = line.split("\\s");
					return parts[1];
				}
			}
		} catch (Exception e) {
			System.out.println("Could not determine version");
			e.printStackTrace();
		}
		return null;
	}
	
}
